package com.cskaoyan.mall.pay.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.pay.constants.PayResult;
import com.cskaoyan.mall.pay.constants.PayRetCode;
import com.cskaoyan.mall.pay.dal.entitys.Payment;
import com.cskaoyan.mall.pay.dal.persistence.PaymentMapper;
import com.cskaoyan.mall.pay.form.PaymentRequest;
import com.cskaoyan.mall.pay.remote.OrderApiClient;
import com.cskaoyan.mall.pay.service.PayCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.Date;

@Service
public class PayCoreServiceImpl implements PayCoreService {

    @Autowired
    PayHelper payHelper;

    @Autowired
    PaymentMapper paymentMapper;

    @Autowired
    OrderApiClient orderApiClient;

    @Override
    public String aliPay(PaymentRequest request) {
        // 1. 获取支付二维码
        String qrCode = payHelper.test_trade_precreate(request);
        if (qrCode == null) {
            // 获取支付二维码失败
            throw new BizException(PayRetCode.GET_CODE_FALIED.getCode(),
                    PayRetCode.GET_CODE_FALIED.getMsg());
        }

        // 要在tb_payment 中针对待支付的订单，插入一条支付记录，状态是 TRADE_PROCESSING
        Payment payment = new Payment();
        Timestamp timestamp = new Timestamp(new Date().getTime());
        payment.setCreateTime(timestamp);
        payment.setStatus(PayResult.TRADE_PROCESSING.getCode());
        payment.setOrderId(request.getOrderId());
        payment.setPayerAmount(request.getMoney());
        payment.setProductName(request.getInfo());
        payment.setPayWay(request.getPayType());
        payment.setPayerUid(request.getUserId());
        payment.setPayerName(request.getNickName());
        payment.setOrderAmount(request.getMoney());
        payment.setRemark(request.getSubject());

        int i = paymentMapper.add(payment);
        if (i < 1){
            throw new BizException(PayRetCode.DB_EXCEPTION.getCode(),PayRetCode.DB_EXCEPTION.getMsg());
        }
        return qrCode;
    }

    @Override
    public boolean queryAlipayRet(String orderId) {
        boolean success = payHelper.test_trade_query(orderId);
        if (success) {
            // 1. 更新tb_payment, 支付状态 TRADE_SUCCESS， 支付完成时间，支付成功时间
            Example example = new Example(Payment.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("orderId",orderId);
            Payment payment = new Payment();
            Timestamp timestamp = new Timestamp(new Date().getTime());
            payment.setPaySuccessTime(timestamp);
            payment.setCompleteTime(timestamp);
            payment.setStatus(PayResult.TRADE_SUCCESS.getCode());
            paymentMapper.updateByExampleSelective(payment,example);
            //   订单服务的功能
            // 2. 修改订单状态为已支付
            orderApiClient.payOrderSuccess(orderId);

            // 3. 查询订单中，所有的订单商品条目，针对每一个条目下单的数量，扣减lock_count
            return true;
        } else {
            // 支付失败
            // 1. 更新tb_payment, 支付状态 TRADE_FAILURE， 支付完成时间
            Example example = new Example(Payment.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("orderId",orderId);
            Payment payment = new Payment();
            Timestamp timestamp = new Timestamp(new Date().getTime());
            payment.setCompleteTime(timestamp);
            payment.setStatus(PayResult.TRADE_FAILURE.getCode());
            paymentMapper.updateByExampleSelective(payment,example);
        }
        return false;
    }
}
